package cn.yomigaeri.warn.controller;

import cn.yomigaeri.common.entity.dto.SignalDTO;
import cn.yomigaeri.common.entity.dto.WarnSignalDTO;
import cn.yomigaeri.common.entity.po.Warn;
import cn.yomigaeri.common.entity.vo.WarnVO;
import cn.yomigaeri.common.result.Result;
import cn.yomigaeri.warn.service.WarnSignalService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WarnSignalController
 * @Package: cn.yomigaeri.warn.controller
 * @Description:
 * @Author Yomigaeri
 * @Create 2025/6/5 00:58
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
public class WarnSignalController {
    private final WarnSignalService warnSignalService;

    @PostMapping("/api/warn/signal")
    public Result warnSignal(@RequestBody List<WarnSignalDTO> warnSignalDTOList) {
        //1. 封装参数
        ObjectMapper objectMapper = new ObjectMapper();
        List<SignalDTO> signalList = new ArrayList<>();
        try {
            for (WarnSignalDTO warnSignalDTO : warnSignalDTOList) {
                SignalDTO signalDTO = objectMapper.readValue(warnSignalDTO.getSignal(), SignalDTO.class);
                signalDTO.setCarId(warnSignalDTO.getCarId());
                signalDTO.setWarnId(warnSignalDTO.getWarnId());
                signalList.add(signalDTO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("参数有误");
        }
        //2. 调用服务
        WarnVO warns = warnSignalService.warnSignal(signalList);
        return Result.success(warns);
    }
}
